<template>
<div style="height: 100%">
  <el-container class="home-container">
<!--    头部区域-->
    <el-header>
      <div>
        <img src="../../assets/logo.jpeg"/>
        <span>电商系统</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button></el-header>
<!--    页面主题-->
    <el-container>
<!--      侧边栏-->
      <el-aside :width="isCollapse ? '64px': '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
<!--        菜单-->
<!--        unique-opened表示为是否只有一个子菜单展开-->
        <el-menu
          :collapse="isCollapse"
          :collapse-transition="false"
          :unique-opened="true   "
        background-color="#333744"
        text-color="#fff"
        style="border: 0px;"
        active-text-color="#409EFF"
        :router="true"
          :default-active="activePath"
        >
        <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
<!--          一级菜单模板区-->
          <template slot="title">
            <i :class="icons[item.id]"></i>
            <span>{{item.authName}}</span>
          </template>
<!--          二级-->
          <el-menu-item @click="SaveNavState('/'+subItem.path)" :index="'/'+subItem.path" :key="subItem.id" v-for="subItem in item.children">
            <i class="el-icon-menu"></i>
            <span>{{subItem.authName}}</span></el-menu-item>
        </el-submenu>
      </el-menu></el-aside>
      <el-container>
        <el-main>
<!--          内容区-->
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</div>
</template>
<script>
export default {
  name: 'index',
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('path')
  },
  data () {
    return {
      menulist: [],
      // 用对象的方式
      icons: {
        125: 'iconfont icon-users',
        126: 'iconfont icon-tijikongjian',
        127: 'iconfont icon-danju',
        128: 'iconfont icon-baobiao',
        129: 'iconfont icon-shangpin'
      },
      isCollapse: false,
      // 路径属性菜单处于高亮的状态（被激活的菜单栏）
      activePath: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单
    getMenuList () {
      // 通过访问接口拿到数据由前面进行筛选出data重命名为res
      // const {data: res} = await this.$http.get('menus')
      const data = [{
        authName: '用户管理',
        id: 125,
        path: 'user',
        children: [{
          authName: '用户列表',
          id: 11,
          path: 'user'
        }]
      },
      {
        authName: '商品管理',
        id: 129,
        path: 'good',
        children: [{
          authName: '商品列表',
          id: 21,
          path: 'list'
        },
        {
          authName: '分类参数',
          id: 22,
          path: 'params'
        },
        {
          authName: '商品分类',
          id: 23,
          path: 'goods'
        }]
      },
      {
        authName: '权限管理',
        id: 126,
        path: 'rights',
        children: [{
          authName: '角色列表',
          id: 31,
          path: 'power'
        }, {
          authName: '权限列表',
          id: 32,
          path: 'jurisdiction'
        }
        ]
      },
      {
        authName: '订单管理',
        id: 127,
        path: 'orders',
        children: [{
          authName: '订单',
          id: 41,
          path: 'orders'
        }]
      },
      {
        authName: '数据统计',
        id: 128,
        path: 'reports',
        children: [{
          authName: '数据报表',
          id: 51,
          path: 'reports'
        }]
      }
      ]
      this.menulist = data
    },
    // 折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    SaveNavState (path) {
      window.sessionStorage.setItem('path', path)
      this.activePath = path
    }
  }
  // ,
  // destroyed () {
  //   window.sessionStorage.removeItem('path')
  // }
}
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
.el-header{
  background-color:rgb(1,1,1) ;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  >div{
    display: flex;
    align-items: center;
    span{
      margin-left: 20px;
    }
  }
}
  .el-aside{
    background: #333744;
  }
  .el-main{
    background: #EAEDF1;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle-button{
    background-color: #4A5064;
    height: 30px;
    font-size: 10px;
    line-height: 30px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;/*文字间距*/
    cursor: pointer;
  }
</style>
